/**
 * Module dependencies.
 */

var config = require('./config');

if (!config.debug && config.oneapm_key) {
    require('oneapm');
}

require('colors');
var path = require('path');
//var Loader = require('loader');
//var LoaderConnect = require('loader-connect')
var express = require('express');
var session = require('express-session');
var passport = require('passport');
require('./middlewares/mongoose_log'); // 打印 mongodb 查询日志
require('./models');
//var GitHubStrategy = require('passport-github').Strategy;
//var githubStrategyMiddleware = require('./middlewares/github_strategy');
var webRouter = require('./web_router');
var apiRouterV1 = require('./api_router_v1');
var auth = require('./middlewares/auth');
var errorPageMiddleware = require('./middlewares/error_page');
var proxyMiddleware = require('./middlewares/proxy');
var RedisStore = require('connect-redis')(session);
var _ = require('lodash');
var csurf = require('csurf');
var compress = require('compression');
var bodyParser = require('body-parser');
var busboy = require('connect-busboy');
var errorhandler = require('errorhandler');
var cors = require('cors');
var requestLog = require('./middlewares/request_log');
var renderMiddleware = require('./middlewares/render');
var logger = require('./common/logger');
var helmet = require('helmet');
var bytes = require('bytes')


// 静态文件目录
var staticDir = path.join(__dirname, 'public');
var bower_componentsDire = path.join(__dirname,'bower_components');
// assets
var assets = {};

if (config.mini_assets) {
    try {
        assets = require('./assets.json');
    } catch (e) {
        logger.error('You must execute `make build` before start app when mini_assets is true.');
        throw e;
    }
}

var urlinfo = require('url').parse(config.server.host);
config.hostname = urlinfo.hostname || config.server.host;

var app = express();

// configuration in all env
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'html');
app.engine('html', require('ejs-mate'));
app.locals._layoutFile = 'layout.html';
app.enable('trust proxy');

// Request logger。请求时间
app.use(requestLog);

if (config.debug) {
    // 渲染时间
    app.use(renderMiddleware.render);
}

// 静态资源
if (config.debug) {
    //app.use(LoaderConnect.less(__dirname)); // 测试环境用，编译 .less on the fly
}
app.use('/public', express.static(staticDir));
app.use('/bower',express.static(bower_componentsDire));

app.use('/agent', proxyMiddleware.proxy);

// 通用的中间件
app.use(require('response-time')());
app.use(helmet.frameguard('sameorigin'));
app.use(bodyParser.json({limit: '1mb'}));
app.use(bodyParser.urlencoded({extended: true, limit: '1mb'}));
app.use(require('method-override')());
app.use(require('cookie-parser')(config.session_secret));
app.use(compress());
app.use(session({
    secret: config.session_secret,
    store: new RedisStore({
        port: config.database.redis.redis_port,
        host: config.database.redis.redis_host,
        pass:config.database.redis.redis_ak + '-' + config.database.redis.redis_sk + '-' + config.database.redis.redis_name
    }),
    resave: false,
    saveUninitialized: false,
}));

// app.use(session({
//     secret:config.session_secret,
//     key:config.auth_cookie_name,
//     cookie:{maxAge:1000*60*60*24*30},
//     resave:false,
//     saveUninitialized:true,
//     store:{
//         db:config.database.mongodb.db_name,
//         host:config.database.mongodb.db_host,
//         port:config.database.mongodb.db_port
//     }
// }));

// oauth 中间件
app.use(passport.initialize());

// github oauth
passport.serializeUser(function (user, done) {
    done(null, user);
});
passport.deserializeUser(function (user, done) {
    done(null, user);
});

//passport.use(new GitHubStrategy(config.GITHUB_OAUTH, githubStrategyMiddleware));

// custom middleware
app.use(auth.authUser);
app.use(auth.blockUser());

if (!config.debug) {
    app.use(function (req, res, next) {
        if (req.path === '/api' || req.path.indexOf('/api') === -1) {
            csurf()(req, res, next);
            return;
        }
        next();
    });
    app.set('view cache', true);
}

// for debug
// app.get('/err', function (req, res, next) {
//   next(new Error('haha'))
// });

// set static, dynamic helpers
_.extend(app.locals, {
    config: config,
    //Loader: Loader,
    assets: assets
});

app.use(errorPageMiddleware.errorPage);
_.extend(app.locals, require('./common/render_helper'));
app.use(function (req, res, next) {
    res.locals.csrf = req.csrfToken ? req.csrfToken() : '';
    next();
});

app.use(busboy({
    limits: {
        fileSize: bytes(config.file_limit)
    }
}));

// routes
app.use('/api/v1', cors(), apiRouterV1);
app.use('/', webRouter);

// error handler
if (config.debug) {
    app.use(errorhandler());
} else {
    app.use(function (err, req, res, next) {

        return res.status(500).send('500 status');
    });
}

if (!module.parent) {
    app.listen(config.server.port, function () {
        logger.info('NodeClub listening on port', config.server.port);
        logger.info('God bless love....');
        logger.info('You can debug your app with http://' + config.server.host + ':' + config.server.port);
        logger.info('');
    });
}

module.exports = app;
